package cn.kone.blog.controller;


import cn.kone.base.utlis.R;
import cn.kone.blog.domain.Param;
import cn.kone.blog.domain.query.ParamQuery;
import cn.kone.blog.service.IParamService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/param")
@Api(tags = "文章接口类")
public class ParamController {
    @Autowired
    private IParamService paramService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部")
    public R<List<Param>> findAllParams() {

        List<Param> all = paramService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个")
    public R<Param> findOneParams(@PathVariable("id") Long id) {
        Param one = paramService.findOne(id);
        return R.ok(one);
    }

    @PutMapping
    @ApiOperation(value = "新增和修改")
    public R<Void> addAndUpdateParam(@RequestBody Param param) {
        Long id = param.getId();
        try {
            if (id == null) {
                paramService.addParam(param);
            } else {
                paramService.updateParam(param);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个")
    public R<Void> deleteParams(@PathVariable("id") Long id) {
        try {
            paramService.deleteParam(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param paramQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
    public PageInfo<Param> pageQuery(@RequestBody ParamQuery paramQuery) {

        List<Param> list = paramService.pageQuery(paramQuery);
        return new PageInfo<Param>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public R<Void> batchDeleteParam(@RequestBody Long[] ids) {
        try {
            paramService.batchDeleteParam(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }


    /**
     * 获取博客参数
     * @return
     */
    @GetMapping("/paramAll")
    @ApiOperation(value = "博客参数")
    public Map<String, String> paramAll() {
        return paramService.paramAll();
    }


}
